﻿using System;
using System.Linq;
using Coolite.Ext.Web;
using Pap.Web.Modules.Reports.ViewModel;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Reports.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class WaitingList : BasePage<WaitingListReportViewModel>
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Master.GenerateReport += Master_GenerateReport;
            if (!Ext.IsAjaxRequest)
            {
                FillReportParams();
            }
        }

        protected void Master_GenerateReport(object sender, EventArgs e)
        {
            var selectedCommittees = lstCommitte.SelectedItems.ToList().Select(x => x.Value).ToList();
            var selectedProjects = lstProjects.SelectedItems.ToList().Select(x => x.Value).ToList();

            this.strReport.DataSource = ViewModel.GetWaitingList(selectedProjects, selectedCommittees);
            this.strReport.DataBind();
        }

        /// <summary>
        /// Fills the report params.
        /// </summary>
        private void FillReportParams()
        {
            lstCommitte.Items.Clear();
            strCommittee.DataSource = this.ViewModel.GetAllCommittee();
            strCommittee.DataBind();

            lstProjects.Items.Clear();
            strProjects.DataSource = this.ViewModel.GetAllProjects();
            strProjects.DataBind();
        }
    }
}
